package com.hs.monitor;

import atlantafx.base.theme.PrimerLight;
import com.hs.monitor.communication.Mqtt;
import com.hs.monitor.entity.Code;
import com.hs.monitor.entity.MessageEntity;
import com.hs.monitor.entity.Para;
import com.hs.monitor.message.Receiver;
import com.hs.monitor.message.Sender;
import com.hs.monitor.utils.ByteUtil;
import com.hs.monitor.utils.LogUtil;
import com.hs.monitor.utils.TaskUtil;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.nio.charset.Charset;

/**
 * Hello world!
 */
public class App extends Application {

//    // 创建工具栏
//    private static ToolBar toolBar = new ToolBar();
//    // 创建菜单栏
//    private static MenuBar menuBar = new MenuBar();
//
//    public static BorderPane root = new BorderPane();

    public static TextField sn;

    @Override
    public void start(Stage primaryStage) {
//        history = new History();

        ToolBar toolBar = new ToolBar();


        Button mqtt = new Button("MQTT");
        Button socket = new Button("Socket");
        toolBar.getItems().addAll(mqtt, socket);


        Label host_label = new Label("主机:");
        TextField host_input = new TextField("192.168.10.101");
        Label port_label = new Label("端口:");
        TextField port_input = new TextField("1883");
        Label sn_label = new Label("SN码:");
        TextField sn_input = new TextField("charger-00001");
        sn = sn_input;

        Label topic_pub_label = new Label("发布主题:");
        TextField topic_pub_input = new TextField("device/data");
        Label topic_sub_label = new Label("订阅主题:");
        TextField topic_sub_input = new TextField("device/data/reply/" + sn_input.getText());

        Button button = new Button("连接");
//        topic.getChildren().addAll(topic_pub_label, topic_pub_input, topic_sub_label, topic_sub_input);

        TextArea textArea_send = new TextArea();
        textArea_send.setPrefHeight(300);

        TextArea textArea_get = new TextArea();
        textArea_get.setPrefHeight(100);

        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        gridPane.add(host_label, 0, 0);
        gridPane.add(host_input, 1, 0);
        gridPane.add(port_label, 2, 0);
        gridPane.add(port_input, 3, 0);
        gridPane.add(sn_label, 4, 0);
        gridPane.add(sn_input, 5, 0);

        gridPane.add(topic_pub_label, 0, 1);
        gridPane.add(topic_pub_input, 1, 1);
        gridPane.add(topic_sub_label, 2, 1);
        gridPane.add(topic_sub_input, 3, 1);
        gridPane.add(button, 5, 1);


        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setPadding(new Insets(10));
        vBox.getChildren().addAll(toolBar, gridPane, textArea_send, textArea_get);

        LogUtil.textArea_send = textArea_send;
        LogUtil.textArea_get = textArea_get;

        button.setOnAction(e -> {
            String sn = sn_input.getText();
            Para para = Para.builder().host(host_input.getText()).port(port_input.getText()).sn(sn_input.getText())
                    .topicPublish(topic_pub_input.getText())
                    .topicSubscribe(topic_sub_input.getText()).build();
            //创建
            Mqtt.createClient(para);

            byte[] sn_bytes = sn.getBytes(Charset.forName("UTF-8"));

            MessageEntity entity = MessageEntity.builder().functionCode(Code.LOGIN).frameNumber(ByteUtil.intToBytes(0)).data(sn_bytes).build();

            byte[] bytes = MessageEntity.getBytes(entity);

            Sender.topic = para.getTopicPublish();

            Sender.send(bytes);

        });

        Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());

        // 设置主场景
        Scene scene = new Scene(vBox, 1080, 600);

        vBox.getStyleClass().add("background");


        //设置背景图
//        scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());

        //应用名称
        primaryStage.setTitle("模拟设备");

        // 设置应用图标
        primaryStage.getIcons().add(new Image("logo.png"));

        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setOnCloseRequest(event -> {
            //关闭线程池
            Receiver.isStop = true;
            //断开mqtt连接
            Mqtt.disconnect();
            TaskUtil.getExecutor().shutdown();
            // 关闭窗口
            primaryStage.close();
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

}
